var Base = function(){
	return {
		alert: function(content, fun) {
			var r = (Math.random() +'').replace(/\./g, '');
			if (this.$alert) {
				this.$alert.find('.modal-body').text(content);
				//this.$alert.remove();
				//$('.modal-backdrop:eq(0)').remove();
			}else{
				var id = 'alert'+ r;
				var alert = '<div class="modal fade bs-example-modal-sm '+ id +'">'+
				'<div class="modal-dialog modal-sm">'+
				'<div class="modal-content">'+
				'<div class="modal-header">'+
				'<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
				'<h4 class="modal-title">提示</h4>'+
				'</div>'+
				'<div class="modal-body">'+ content +'</div>'+
				'<div class="modal-footer">'+
				'<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>'+
				'</div>'+
				'</div></div></div>';
				$(document.body).append(alert);
				this.$alert = $('.'+ id);
			}
			if (typeof fun == 'function') {
				this.$alert.find('.modal-footer button').unbind('click').click(function() {
					fun.call(this);
				});
			}
			this.$alert.modal('show');
			this.$alert.css("z-index", r); //解决多个弹窗同时调用时出现层覆盖问题
		},
		confirm: function(content, fun) {
			var r = (Math.random() +'').replace(/\./g, '');
			if (this.$confirm) {
				this.$confirm.find('.modal-body').text(content);
			} else {
				var id = 'confirm'+ (Math.random() +'').replace(/\./g, '');
				var confirm = '<div class="modal fade bs-example-modal-sm '+ id +'">'+
				'<div class="modal-dialog modal-sm">'+
				'<div class="modal-content">'+
				'<div class="modal-header">'+
				'<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
				'<h4 class="modal-title">提示</h4>'+
				'</div>'+
				'<div class="modal-body">'+ content +'</div>'+
				'<div class="modal-footer">'+
				'<button type="button" class="btn btn-primary">确定</button>'+
				'<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
				'</div>'+
				'</div></div></div>';
				$(document.body).append(confirm);
				this.$confirm = $('.'+ id);
			}
			this.$confirm.modal('show');
			this.$confirm.find('.btn-primary').unbind('click').click(function() {
				fun.call(this);
				Base.$confirm.modal('hide');
			});
			this.$confirm.css("z-index", r); //解决多个弹窗同时调用时出现层覆盖问题
		},
		modelWin : function(config){
			var conf = {
				title : '标题',
				content: '',
				buttons: null
			};
			$.extend(conf, config);
			if(this.$modelWin){
				this.$modelWin.find('.modal-title').html(conf.title);
				this.$modelWin.find('.modal-body').html(conf.content);
			}else{
				var id = 'window_'+ (Math.random() +'').replace(/\./g, '');
				var bodyHTML = '<div class="modal fade '+id+'">';
					bodyHTML += '<div class="modal-dialog">';
					bodyHTML += '<div class="modal-content">';
					bodyHTML += '<div class="modal-header">';
					bodyHTML += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
					bodyHTML += '<h4 class="modal-title" >'+conf.title+'</h4>';
					bodyHTML += '</div>';
					bodyHTML += '<div class="modal-body form-box formz clearfix"></div>';
					bodyHTML += '<div class="modal-footer">';
					bodyHTML += '</div>';
					bodyHTML += '</div>';
					bodyHTML += '</div>';
					bodyHTML += '</div>';
				$(document.body).append(bodyHTML);
				this.$modelWin = $('.'+ id);
				this.$modelWin.find('.modal-body').html(conf.content);
			}
			if (conf.buttons) {
				var btnArr = conf.buttons, $footer = this.$modelWin.find('.modal-footer');
				$footer.html('');
				for (var i = 0; i < btnArr.length; i++) {
					var $btn = $('<button type="button" class="btn btn-primary">'+ btnArr[i].name +'</button>').appendTo($footer);
					this.callback = btnArr[i].fun;
					$btn.click(function() {
						if(Base.callback.call(this)){
							Base.$modelWin.modal('hide');
						}
					});
				};
				$footer.append('<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>');
			}
			this.$modelWin.modal('show');
		},
		loading: function (bl) {
			if (!this.$load) {
				var id = 'alert'+ (Math.random() +'').replace(/\./g, '');
				var alert = '<div class="modal fade bs-example-modal-sm '+ id +'">'+
				'<div class="modal-dialog modal-sm">'+
				'<div class="modal-content">'+
				'<div class="modal-body">处理中...</div>'+
				'</div></div></div>';
				$(document.body).append(alert);
				this.$load = $('.'+ id);
			}
			if (bl)
				this.$load.modal({backdrop: 'static', show: true, keyboard: false});
			else
				this.$load.modal('hide');
		},
		addDate: function (date,add){
			var a = new Date(date);
				a = a.valueOf();
				a = a + add * 24 * 60 * 60 * 1000;
				a = new Date(a);
			return a;
		},
		getDateDiff : function (startDate,endDate) {
	        var startTime = new Date(Date.parse(startDate.replace(/-/g,"/"))).getTime();
	        var endTime = new Date(Date.parse(endDate.replace(/-/g,"/"))).getTime();
	        var dates = Math.abs((startTime - endTime))/(1000*60*60*24);
	        return  dates+1;    
	    },
	    compareDate : function (startDate,endDate) {
	        var startTime = new Date(Date.parse(startDate.replace(/-/g,"/"))).getTime();
	        var endTime = new Date(Date.parse(endDate.replace(/-/g,"/"))).getTime();
	        if(startTime>endTime){
	            return 1;
	        } else {
	            return 0;
	        }
	    },		
		openWindow:function(url,name,iWidth,iHeight){                
			var iTop = (window.screen.height-30-iHeight)/2; //获得窗口的垂直位置;  
	        var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位置;  
	        var style = 'height=' + iHeight + ',innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',toolbar=no,menubar=no,scrollbars=yes,resizeable=yes,location=no,status=no';
	        var newWin = window.open(url, name, style);
	        try{
        		window.onfocus = function () { newWin.focus(); };
	            window.onclick = function () { newWin.focus(); };
	        }catch(e){
	        }
	        return newWin;
		},
		/**
		 * @Description window 以post方式提交参数。
		 * @Param url 		请求URL
		 * @Param title 	window title
		 * @Param width 	window 宽
		 * @Param height 	window 高
		 * @Param param 	URL 的 POST 请求参数 param 格式 ：{"name1":"value1", "name2":"value2", ...}
		 */
		postOpenWindow : function(url, width, height, param, title){
			if(!url){
				Base.alert("URL is enmpty ");
				return;
			}
			//若宽，高参数未设定，设置默认的宽高。
			if(!width){
				width = 1000;
			}
			if(!height){
				height = 680;
			}
			//若title参数未设定，设置默认的title。
			if(!title){
				title = "open_window_title_"+(Math.random() +'').replace(/\./g, '');
			}
			
			var opWin = Base.openWindow('about:blank', title, width, height);
			//动态创建一个form
			var tempForm = opWin.document.createElement("form");
			tempForm.id = "open_window_form_"+(Math.random() +'').replace(/\./g, '');
			tempForm.method ="post";
			tempForm.action = url;
			tempForm.target = title;
			
			//参数处理
			if(param){
				for(var key in param){
					var input = "hideInput_"+key;
					input = opWin.document.createElement("input");
					input.type = "hidden";
					input.name = key;
					input.value = param[key];
					tempForm.appendChild(input);
				}
				//将动态创建的form添加到document.body中
				document.body.appendChild(tempForm);
				//自动提交form
				tempForm.submit();
				//将document.body中动态创建的form移除
			    document.body.removeChild(tempForm);	
			}
		}
	};
}();
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function formatDate(fdate){
	if(fdate){
		return new Date(fdate).Format("yyyy-MM-dd");
	} else {
		return "";
	}
}
function dateForamtToString(fdate){
	if(fdate){
		return new Date(fdate).Format("yyyy-MM-dd hh:mm:ss");
	} else {
		return "";
	}
}

//格式化金额自动四舍五入 s:源金额数据 , n:保留小数位数
//调用：formatMoney("12345.675910", 3)，返回12,345.676 
formatMoney = function(s, n){
	if ('number' != typeof(s) || 'number' != typeof(n)){
		var result = "";
		if ('number' == typeof(s) && n > 0) {
			result = "0."
			for (var i = 0; i < n; i++){
				result += "0";
			}
		}
		return result;
	}		
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
	t = "";
	for (i = 0; i < l.length; i++) {
		t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	}
	return t.split("").reverse().join("") + "." + r;
}

//将所有红色字段值组成字符串，在保存时在获得一次，比较两次的值，如果不同则审核字段为未审核，否则不变
function concatRedAttr(){
	var changeStr = "";
	$("input[type=text][redMark=isAudit]").each(function(index){
		changeStr +=$(this).val();
	})
	$("input[type=radio][redMark=isAudit]").each(function(index){
		changeStr +=$(this).prop("checked");
	})
	$("input[type=checkbox][redMark=isAudit]").each(function(index){
		changeStr +=$(this).prop("checked");
	})
	$("select[redMark=isAudit]").each(function(index){
		changeStr +=$(this).val();
	})
	return changeStr;
}